<template>
  <div>
    <van-nav-bar
      title="账号登入"
      left-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onClickLeft()">
      <div class="kku"></div>
      <van-field
        v-model="user.username"
        name="用户名"
        placeholder="请输入用户名哦~~"
        size:.625rem
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <div class="kku"></div>
      <van-field
        v-model="user.password"
        type="password"
        name="密码"
        placeholder="请输入密码哦~~~"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          square
          block
          type="info"
          native-type="submit"

          color="#1cb676"
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="程序员拼命加载ing..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  data () {
    return {
      user: {
        username: 'hzhmqd',
        password: '123456'
      },
      isLoading: false
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onClickLeft () {
      try {
        await this.asyncLoginAction(this.user)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoading = false
        this.$router.replace('/home/profile')
      } catch (err) {
        console.error(err)
        this.isLoading = false
        Notify({ type: 'warning', message: '手机号或密码错误' })
      }
    }
  }
}
</script>

<style >
van-field {
  margin-top: 100px;
}
.kku {
  height: 20px;
}
</style>
